class MetaController{
    async post_level1(req,res){
        console.log(req.body)
        var title=String(req.body.title)
        var level1=await global.prisma.level1.create({
            data:{
                title:title
            }
        })
        res.send(200,level1)
    }

    async post_level2(req,res){
        var title=String(req.body.title)
        var level1_id=Number(req.body.level1_id)
        var level1=await global.prisma.level1.findUnique({
            where:{
                id:level1_id
            }
        })
        if(level1==null){
            res.send(400,'一级指标不存在！')
            return
        }
        var level2=await global.prisma.level2.create({
            data:{
                title:title,
                level1_id:level1_id
            }
        })
        res.send(200,level2)
    }

    async post_level3(req,res){
        var title=String(req.body.title)
        var level2_id=Number(req.body.level2_id)
        var score_max=Number(req.body.score_max)
        var level2=await global.prisma.level2.findUnique({
            where:{
                id:level2_id
            }
        })
        if(level2==null){
            res.send(400,'二级指标不存在！')
            return
        }
        var level3=await global.prisma.level3.create({
            data:{
                title:title,
                level2_id:level2_id,
                score_max:score_max
            }
        })
        res.send(200,level3)
    }

    async get_level1(req,res){
        var id=Number(req.params.id)
        var level1=await global.prisma.level1.findUnique({
            where:{
                id: id
            },
            include:{
                level2s:{
                    include:{
                        level3s:true
                    }
                }
            }
        })
        res.send(200,level1)
    }

    async get_level1s(req,res){
        try{
            var level1=await global.prisma.level1.findMany({
                orderBy:{index:'asc'},
                include:{
                    level2s:{
                        orderBy:{index:'asc'},
                        include:{
                            level3s:{
                                orderBy:{index:'asc'}
                            }
                        }
                    }
                }
            })
            res.send(200,level1)
        }catch(err){
            console.error(err)
            res.send(500)
        }
        
    }

    async get_level2(req,res){
        var id=Number(req.params.id)
        var level2=await global.prisma.level2.findUnique({
            where:{
                id: id
            },
            include:{
                level3s: true,
                level1:true
            }
        })
        res.send(200,level2)
    }

    async get_level3(req,res){
        var id=Number(req.params.id)
        var level3=await global.prisma.level3.findUnique({
            where:{
                id: id
            },
            include:{
                level2: {
                    include:{
                        level1:true
                    }
                }
            }
        })
        res.send(200,level3)
    }

    async del_level1(req,res){
        var id=Number(req.params.id)
        var level1=await global.prisma.level1.findUnique({
            where:{id:id},
            include:{
                level2s:{
                    include:{
                        level3s:true
                    }
                }
            }
        })
        try{
            await global.prisma.$transaction(async(tx)=>{
                for(var level2 in level1.level2s){
                    await tx.level3.deleteMany({
                        where:{level2_id:level2.id}
                    })
                }
                await tx.level2.deleteMany({
                    where:{level1_id:id}
                })
                await tx.level1.delete({
                    where:{id:id}
                })
            })
        }catch(err){
            console.error('删除一级指标事务执行失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async del_level2(req,res){
        var id=Number(req.params.id)
        var level2=await global.prisma.level2.findUnique({
            where:{id:id},
            include:{
                level3s:true
            }
        })
        try{
            await global.prisma.$transaction(async(tx)=>{
                await tx.level3.deleteMany({
                    where:{level2_id:id}
                })
                await tx.level2.delete({
                    where:{id:id}
                })
            })
        }catch(err){
            console.error('删除二级指标事务执行失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async del_level3(req,res){
        var id=Number(req.params.id)
        try{
            await global.prisma.level3.delete({
                where:{id:id}
            })
        }catch(err){
            console.error('删除三级指标失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async put_level1(req,res){
        var id=Number(req.params.id)
        var title=req.body.title
        try{
            await global.prisma.level1.update({
                where:{id:id},
                data:{title:title}
            })
        }catch(err){
            console.error('更新一级指标失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async put_level2(req,res){
        var id=Number(req.params.id)
        var title=req.body.title
        try{
            await global.prisma.level2.update({
                where:{id:id},
                data:{title:title}
            })
        }catch(err){
            console.error('更新二级指标失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async put_level3(req,res){
        var id=Number(req.params.id)
        var title=req.body.title
        var score_max=req.body.score_max
        try{
            await global.prisma.level3.update({
                where:{id:id},
                data:{
                    title:title,
                    score_max:score_max
                }
            })
        }catch(err){
            console.error('更新评价细则失败！')
            res.send(500)
            return
        }
        res.send(200)
    }

    async put_level1_index(req,res){
        var old_index=req.body.old_index
        var new_index=req.body.new_index
        try{
            var level1s=await global.prisma.level1.findMany({
                orderBy:{index:'asc'}
            })
            if(old_index<new_index){
                await global.prisma.$transaction(async(tx)=>{
                    await tx.level1.update({
                        where:{id:level1s[old_index].id},
                        data:{index:level1s[new_index].index}
                    })
                    for(var i=old_index+1;i<=new_index;i++){
                        await tx.level1.update({
                            where:{id:level1s[i].id},
                            data:{index:level1s[i-1].index}
                        })
                    }
                })
            }else{
                await global.prisma.$transaction(async(tx)=>{
                    for(var i=new_index;i<old_index;i++){
                        await tx.level1.update({
                            where:{id:level1s[i].id},
                            data:{index:level1s[i+1].index}
                        })
                    }
                    await tx.level1.update({
                        where:{id:level1s[old_index].id},
                        data:{index:level1s[new_index].index}
                    })
                })
            }
            res.send(200)
        }catch(err){
            console.error('调整一级指标顺序失败！')
        }
    }

    async put_level2_index(req,res){
        var level1_id=req.body.level1_id
        var old_index=req.body.old_index
        var new_index=req.body.new_index
        try{
            var level2s=await global.prisma.level2.findMany({
                where:{level1_id:level1_id},
                orderBy:{index:'asc'}
            })
            if(old_index<new_index){
                await global.prisma.$transaction(async(tx)=>{
                    await tx.level2.update({
                        where:{id:level2s[old_index].id},
                        data:{index:level2s[new_index].index}
                    })
                    for(var i=old_index+1;i<=new_index;i++){
                        await tx.level2.update({
                            where:{id:level2s[i].id},
                            data:{index:level2s[i-1].index}
                        })
                    }
                })
            }else{
                await global.prisma.$transaction(async(tx)=>{
                    for(var i=new_index;i<old_index;i++){
                        await tx.level2.update({
                            where:{id:level2s[i].id},
                            data:{index:level2s[i+1].index}
                        })
                    }
                    await tx.level2.update({
                        where:{id:level2s[old_index].id},
                        data:{index:level2s[new_index].index}
                    })
                })
            }
            res.send(200)
        }catch(err){
            console.error('调整二级指标顺序失败！')
        }
    }

    async put_level3_index(req,res){
        var level2_id=req.body.level2_id
        var old_index=req.body.old_index
        var new_index=req.body.new_index
        try{
            var level3s=await global.prisma.level3.findMany({
                where:{level2_id:level2_id},
                orderBy:{index:'asc'}
            })
            if(old_index<new_index){
                await global.prisma.$transaction(async(tx)=>{
                    await tx.level3.update({
                        where:{id:level3s[old_index].id},
                        data:{index:level3s[new_index].index}
                    })
                    for(var i=old_index+1;i<=new_index;i++){
                        await tx.level3.update({
                            where:{id:level3s[i].id},
                            data:{index:level3s[i-1].index}
                        })
                    }
                })
            }else{
                await global.prisma.$transaction(async(tx)=>{
                    for(var i=new_index;i<old_index;i++){
                        await tx.level3.update({
                            where:{id:level3s[i].id},
                            data:{index:level3s[i+1].index}
                        })
                    }
                    await tx.level3.update({
                        where:{id:level3s[old_index].id},
                        data:{index:level3s[new_index].index}
                    })
                })
            }
            res.send(200)
        }catch(err){
            console.error('调整三级指标顺序失败！')
        }
    }
}

module.exports=new MetaController()